0% completed
Problem Statement
You are given a binary array seats representing the row of seats, where seats[i] = 0 represents that the ith seat is empty, and seats[i] = 1 represents a person sitting in the ith seat. It is given that seats array contains at least one 1 and 0.
You want to sit in any seat such that the distance between you and the closest person to you is maximized.
Return that maximum distance to the closest person.
Examples
-
Example 1:
- Input: seats =
[1,0,0,0,1,0,0] - Expected Output:
2 - Justification: The best seat is either of the two at the index 2 or 6, as they are both 2 seats away from the nearest occupied seat.
- Input: seats =
-
Example 2:
- Input: seats =
[1,0,0,0,0,1] - Expected Output:
2 - Justification: The middle seats (index 2 or 3) are equally optimal, being 2 seats away from any occupied seat.
- Input: seats =
-
Example 3:
- Input: seats =
[0,1,0,1,0,1,0,0,0,0] - Expected Output:
4 - Justification: The best position is at the end (index 9), which is 4 seats away from the nearest person.
- Input: seats =
Solution
To solve this problem, our approach focuses on calculating the distance between occupied seats and identifying the maximum gap for a person to sit. Initially, we'll scan the array to find the distances between all pairs of occupied seats. Additionally, we'll pay special attention to the edges of the array, where the distance calculation slightly varies because one side is bound by the array's end rather than another occupied seat. This method ensures we consider the maximum available distance at both the beginning and the end of the row.
The rationale behind this approach is that the optimal seat, in terms of distance to the closest person, is either between two occupied seats, at the beginning, or at the end of the row. By comparing these distances, we can determine the best seat. This method is effective because it directly calculates the relevant distances without unnecessary comparisons or iterations, making it efficient for solving the problem.
Step-by-step Algorithm
-
Initialize Variables:
maxDistanceto keep track of the maximum distance to the closest person. Initially set to 0.lastOccupiedto track the index of the last occupied seat encountered while iterating through the array. Initially set to -1 as a marker that no occupied seat has been encountered yet.nto store the length of the seats array.
-
Iterate Through the Seats:
- Loop through each seat in the array using an index variable
i.
- Loop through each seat in the array using an index variable
-
Check if Seat is Occupied:
- If the current seat (
seats[i]) is occupied (== 1), proceed to calculate the distance to the nearest person. - If it's the first occupied seat encountered (
lastOccupied == -1), the distance isiitself, because it's the distance from the start to the first occupied seat. - Otherwise, calculate the distance as
(i - lastOccupied) / 2. This accounts for the distance between two occupied seats, effectively finding the midpoint which gives the maximum distance to the closest person.
- If the current seat (
-
Update
lastOccupied:- Set
lastOccupiedto the current indexito mark the latest occupied seat.
- Set
-
Check Distance from Last Occupied Seat to End:
- After the loop completes, check if the distance from the last occupied seat to the end of the array (
n - 1 - lastOccupied) is greater than the currentmaxDistance.
- After the loop completes, check if the distance from the last occupied seat to the end of the array (
-
Return
maxDistance:- Return the
maxDistancefound as the maximum distance to the closest person.
- Return the
Algorithm Walkthrough
Let's consider the input [0,1,0,1,0,1,0,0,0,0].
-
Start:
maxDistance = 0,lastOccupied = -1,n = 10
-
Iteration 0 (
i = 0):- Seat is not occupied. No updates.
-
Iteration 1 (
i = 1):- Seat is occupied.
lastOccupied = -1initially, solastOccupiedis updated to 1. UpdatemaxDistanceto1
- Seat is occupied.
-
Iteration 2 (
i = 2):- Seat is not occupied. No updates.
-
Iteration 3 (
i = 3):- Seat is occupied. Calculate distance:
(3 - 1) / 2 = 1.maxDistanceremains 1. UpdatelastOccupiedto 3.
- Seat is occupied. Calculate distance:
-
Iteration 4 (
i = 4):- Seat is not occupied. No updates.
-
Iteration 5 (
i = 5):- Seat is occupied. Calculate distance:
(5 - 3) / 2 = 1.maxDistanceremains 1. UpdatelastOccupiedto 5.
- Seat is occupied. Calculate distance:
-
Iterations 6 to 9:
- Seats are not occupied until the end. No updates to
maxDistanceorlastOccupiedduring these iterations.
- Seats are not occupied until the end. No updates to
-
After Loop:
- Check distance from last occupied to end:
9 - 5 = 4. Since4is greater thanmaxDistance, updatemaxDistanceto 4.
- Check distance from last occupied to end:
-
End:
maxDistance = 4is returned as the maximum distance to the closest person.
Code
Complexity Analysis
Time Complexity:
The algorithm iterates through the array exactly once to calculate the maximum distance to the closest person. Hence, the time complexity is linear with respect to the number of seats, n.
Space Complexity:
The space complexity is constant because the solutions use a fixed amount of space regardless of the input size.
On this page
Problem Statement
Examples
Solution
Step-by-step Algorithm
Algorithm Walkthrough
Code
Complexity Analysis
Time Complexity:
Space Complexity: